package com.dnake.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.dnake.Constant;
import com.dnake.entity.BaseHttpBean;
import com.dnake.entity.EventBusBean;
import com.dnake.entity.EventBusUpdateBean;
import com.dnake.entity.EventSettingBean;
import com.dnake.panel.BuildConfig;
import com.dnake.panel.R;
import com.dnake.utils.ToastUtil;
import com.orhanobut.logger.Logger;
import com.wx.android.common.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author haoyuliu
 * @since 2017/7/18
 */

public class SyncFragment extends SupportFragment {
    private View mView;
    private ProgressDialog progressDialog;
    public static SyncFragment newInstance() {
        return new SyncFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_sync, container, false);
        ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public boolean onBackPressedSupport() {
        return super.onBackPressedSupport();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventSetting(EventSettingBean eventBusBean) {
        if (eventBusBean.getCode()== Constant.EVENTBUS_SYNC){
            if ("1".equals(eventBusBean.getName())){
                uploadData();
            }else if ("2".equals(eventBusBean.getName())){
                EventBus.getDefault().post(new EventBusUpdateBean(""));
            }else if ("3".equals(eventBusBean.getName())){
                EventBus.getDefault().post(new EventBusBean(Constant.EVENTBUS_SYNC_DATA,""));
            }
        }
    }

    /**
     * 同步设备配置数据
     */
    private void uploadData() {
        RequestParams params = new RequestParams(Constant.HTTP_URL+"/community/device/update/config");
        params.addBodyParameter("deviceId", SharedPreferencesUtils.getString(Constant.SP_DEVICEID));
        params.addBodyParameter("videoStatus", (SharedPreferencesUtils.getBoolean(Constant.SP_VIDEO_STATUS)?1:0)+"");
        params.addBodyParameter("lockType", (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_TYPE)?0:1)+"");
        params.addBodyParameter("lockStatus", (SharedPreferencesUtils.getBoolean(Constant.SP_LOCK_STATUS)?1:0)+"");
        params.addBodyParameter("delayLock", SharedPreferencesUtils.getInt(Constant.SP_DELAY_TIME)+"");
        params.addBodyParameter("apkVersion", BuildConfig.VERSION_NAME);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException arg0) {

            }

            @Override
            public void onError(Throwable throwable, boolean arg1) {
                throwable.printStackTrace();
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onSuccess(String response) {
                Logger.json(response);
                if (!TextUtils.isEmpty(response)) {
                    BaseHttpBean httpBean = JSON.parseObject(response, BaseHttpBean.class);
                    if (httpBean.getCode()==200){
                        ToastUtil.showSuccessView("同步成功");
                    }else {
                        ToastUtil.showFailView(httpBean.getMessage());
                    }
                }
            }
        });
    }


    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
